package com.mo2323.merchant.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mo2323.common.VO;
import com.mo2323.common.util.AjaxObject;
import com.mo2323.service.merchant.IVenueRechargeActivityService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

@Controller
@RequestMapping("/venueRecharge")
public class VenueRechargeActivityController extends BaseController {
    @Reference
    private IVenueRechargeActivityService rechargeActivityService;

    //DONE:获取活动详情  ：活动有效期  活动须知
    @RequestMapping(value = "/activityDetail", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject getRechargeActivityDetail(@RequestParam Integer vid, @RequestParam Integer mid) {
        try {
            VO rechargeActivityDetail = rechargeActivityService.getRechargeActivityDetail(vid, mid);
            return AjaxObject.ok().data(rechargeActivityDetail.toMap());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("getRechargeActivityDetailError", e.getMessage());
            return AjaxObject.error(e.getMessage());
        }
    }

    /**
     * 充值200送200活动--微信支付
     */
    @RequestMapping(value = "/rechargeActivityWxPay", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject rechargeActivityWxPay(Integer vid, Integer mid, String wxAppletCode) {
        try {
            VO result = rechargeActivityService.rechargeActivityWxPay(vid, mid, wxAppletCode);
            return AjaxObject.ok().data(result.toMap());
        } catch (Exception e) {
            logger.error("rechargeActivityWxPay", e.getMessage());
            return AjaxObject.error(e.getMessage());
        }
    }

    /**
     * 充值200送200活动--微信支付回调
     */
    @RequestMapping(value = "/rechargeActivityWxPayNotify", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String rechargeActivityWxPayNotify(HttpServletRequest request) {
        try {
            System.out.println("---------------充值200送200微信回调-------------------");
            InputStream inStream = request.getInputStream();
            ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inStream.read(buffer)) != -1) {
                outSteam.write(buffer, 0, len);
            }
            String resultXml = new String(outSteam.toByteArray(), "utf-8");
            System.out.println(resultXml);
            outSteam.close();
            inStream.close();
            return rechargeActivityService.doRechargeActivityWxPayNotify(resultXml);
        } catch (Exception e) {
            logger.error("rechargeActivityWxPay", e.getMessage());
            return e.getMessage();
        }
    }
}